AE = typeof AE !== 'undefined' ? AE : {};
AE.OptionsHelper = (function() {
    var backgroundPage = chrome.extension.getBackgroundPage(),
        PrefsHelper = backgroundPage.AE.PrefsHelper,
        Expanders = backgroundPage.AE.Expanders,
        Helper = AE.Helper;

    var chk_url = "http://tinyurl.com/161", // http://www.google.com/ use tinyurl.com
        chk_realurl = "http://www.google.com/";

    var showCheckMsg = function(el, msg, color) {
        el.innerHTML = msg;
        el.style.color = color || "#404040";
    };

    var MESSAGES = {
        startCheck : "using <strong>{expander}</strong> to expand 'http://tinyurl.com/161' to 'http://www.google.com/' ...",
        responseOK : "<strong>{expander}</strong> conrrectly responsed in <strong>{time}s</strong>.",
        responseErr : "<strong>{expander}</strong> inconrrectly responsed in <strong>{time}s</strong>, need <strong>'{realUrl}'</strong>, but returned <strong><em>'{realUrl2}'</em></strong>.",
        expandTimeout : "<strong>{expander}</strong> expands short url timeout in 10s, maybe the service is unavailable."
    };

    var OptionsHelper = {
        restore : function(doc) {
            PrefsHelper.renderUI(doc);
        },

        save : function(doc) {
            PrefsHelper.saveFromUI(doc);
        },

        checkAll : function(msgContl) {
            msgContl.innerHTML = "";
            var expanders = Expanders.getExpanders();
            expanders.forEach(function(expander) {
                var el = document.createElement("SPAN");
                msgContl.appendChild(el);
                OptionsHelper.checkAvailable(expander, el);
            });
        },

        checkAvailable : function(expander, msgContl) {
            (function(expander, msgContl) {
                var chk_timer, chk_starttime;

                chk_starttime = +new Date();
                showCheckMsg(msgContl, Helper.format(MESSAGES.startCheck, {
                    expander : expander
                }));
                Expanders.use(expander, "http://tinyurl.com/161", function(realUrl) {
                    clearTimeout(chk_timer);
                    var time = (((+new Date()) - chk_starttime)/1000).toFixed(1);
                    if (realUrl === chk_realurl) {
                        showCheckMsg(msgContl, Helper.format(MESSAGES.responseOK, {
                            expander : expander,
                            time : time
                        }), "green");
                    } else {
                        showCheckMsg(msgContl, Helper.format(MESSAGES.responseErr, {
                            expander : expander,
                            time : time,
                            realUrl : chk_realurl,
                            realUrl2 : realUrl
                        }), "#CC0000");
                    }
                });
                chk_timer = setTimeout(function() {
                    showCheckMsg(msgContl, Helper.format(MESSAGES.expandTimeout, {
                        expander : expander
                    }), "#f60");
                }, 10000);
            })(expander, msgContl);
        }
    };

    return OptionsHelper;
})();
